package com.decent.manager.system.dao;

import com.decent.common.annotation.ReturnCheck;
import com.decent.common.entity.manager.SystemMenu;
import com.decent.manager.system.dao.provider.SystemMenuDaoProvider;
import com.decent.manager.system.dto.SystemMenuDTO;
import com.decent.manager.system.vo.SystemMenuVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 系统资源dao
 *
 * @author wangyan
 **/
@Mapper
public interface SystemMenuDao {

    /**
     * 查询菜单
     *
     * @param dto 条件
     * @return 列表
     */
    @SelectProvider(type = SystemMenuDaoProvider.class, method = "list")
    List<SystemMenuVO> list(@Param("dto") SystemMenuDTO dto);

    /**
     * 根据id查询
     *
     * @param id id
     * @return 结果
     */
    @Select("select * from system_menu where id = #{id} ")
    SystemMenu getById(@Param("id") Long id);

    /**
     * 新增资源
     *
     * @param systemMenu 新增参数
     * @return 影响行数
     */
    @SuppressWarnings("UnusedReturnValue")
    @Insert("insert into system_menu " +
            "(pid, pids, menu_name, menu_code, menu_type, icon, router, component, " +
            "permission, visibility, query_route, redirect, weight, menu_sort, remark, menu_status, create_time, " +
            "creator, update_time, last_modifier) " +
            "VALUES " +
            "(#{sysMenu.pid}, #{sysMenu.pids}, #{sysMenu.menuName}, #{sysMenu.menuCode}, #{sysMenu.menuType}, " +
            "#{sysMenu.icon}, #{sysMenu.router}, #{sysMenu.component}, #{sysMenu.permission}, #{sysMenu.visibility}, " +
            "#{sysMenu.queryRoute}, #{sysMenu.redirect}, #{sysMenu.weight}, #{sysMenu.menuSort}, #{sysMenu.remark}, " +
            "#{sysMenu.menuStatus}, now(), #{sysMenu.creator}, now(), #{sysMenu.creator}) ")
    @ReturnCheck(info = "添加菜单失败")
    int addSystemMenu(@Param("sysMenu") SystemMenu systemMenu);

    /**
     * 删除菜单和子菜单
     *
     * @param pid 父id
     */
    @Delete("delete from system_menu where id = #{pid} or find_in_set('[${pid}]',pids) ")
    void deleteMenu(@Param("pid") Long pid);

    /**
     * 根据节点id获取所有子节点id集合
     *
     * @param id 接点id
     * @return 结果
     */
    @Select("select id from system_menu where find_in_set('[${id}]',pids) ")
    List<Long> getChildIdListById(@Param("id") Long id);

    /**
     * 删除角色菜单
     *
     * @param pids 菜单id
     */
    @Delete("<script>" +
            "delete from system_role_menu where menu_id " +
            " in " +
            "<foreach collection = 'pids' item = 'id' separator = ',' open = '(' close = ')'> " +
            " #{id} " +
            "</foreach>" +
            "</script>")
    void deleteRoleMenuListByMenuId(@Param("pids") List<Long> pids);

    /**
     * 根据名字查询
     *
     * @param name name
     * @return 结果
     */
    @Select("select id from system_menu where menu_name = #{name} and menu_status = 'NORMAL'  ")
    Long nameExist(@Param("name") String name);

    /**
     * 根据code查询
     *
     * @param code code
     * @return 结果
     */
    @Select("select id from system_menu where menu_code = #{code} and menu_status = 'NORMAL'  ")
    Long codeExist(@Param("code") String code);


    /**
     * 更新菜单
     *
     * @param dto 参数
     * @return 结果
     */
    @SuppressWarnings("UnusedReturnValue")
    @Update("update system_menu set pid = #{dto.pid} ,pids = #{dto.pids} ,menu_name = #{dto.menuName} ,menu_code = " +
            "#{dto.menuCode} ," +
            " menu_type = #{dto.menuType} , icon = #{dto.icon} ,router = #{dto.router} ,component = #{dto.component} ,permission = #{dto.permission} ," +
            "  visibility = #{dto.visibility} ,query_route = #{dto.queryRoute} ,redirect = #{dto.redirect} ,weight = #{dto.weight} ," +
            " menu_sort = #{dto.menuSort} ,remark = #{dto.remark} ,update_time = #{dto.updateTime} , last_modifier= #{dto.lastModifier} " +
            " where id = #{dto.id} ")
    @ReturnCheck(info = "资源更新失败")
    int updateMenu(@Param("dto") SystemMenu dto);
}
